﻿#region All usings
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
#endregion

namespace senSimulator.DrawableComponents
{
    public class DrawableComponent
    {
        #region Declaration of basic variables and the constructor
        /// <summary>
        /// Basic variables for drawing items
        /// </summary>
        protected Texture2D[] _textures;
        protected Vector2[] _textureCenters;
        protected Vector2 _texturePosition;
        protected float _rotationInDegrees;


        /// <summary>
        /// Properties and fields for displaying data (might be handy in the near future...)
        /// </summary>
        protected ContentManager content;


        /// <summary>
        /// Constructor to catch all needed components for drawing
        /// </summary>
        /// <param name="textures">All textures that are needed for to draw a basic shape</param>
        /// <param name="posX">The X position in the area</param>
        /// <param name="posY">The Y position in the area</param>
        /// <param name="rotationInDegrees">Rotation in degrees</param>
        public DrawableComponent(Game game, Texture2D[] textures, Vector2[] textureCenters, int posX, int posY, int rotationInDegrees)// : base(game)
        {
            // Set the basic variables
            content = new ContentManager(game.Services);
            _textures = textures;
            _texturePosition = new Vector2(posX, posY);
            _textureCenters = textureCenters;
            _rotationInDegrees = (float)(Math.PI * rotationInDegrees / 180);
        }    
        #endregion


        #region Functions
        /// <summary>
        /// Add the rotation value to the current direction
        /// </summary>
        /// <param name="degrees">rotation</param>
        public void Rotate(int degrees)
        {
            if (degrees%360 == 0)
                _rotationInDegrees += (float)(Math.PI * degrees / 180);
            else
                _rotationInDegrees += (float)(Math.PI * (degrees % 360) / 180);
        }


        /// <summary>
        /// Set the rotation value
        /// </summary>
        /// <param name="degrees"></param>
        public void RotateAbsolute(int degrees)
        {
            if (degrees % 360 == 0)
                _rotationInDegrees = (float)(Math.PI * degrees / 180);
            else
                _rotationInDegrees = (float)(Math.PI * (degrees % 360) / 180);
        }


        /// <summary>
        /// Get the rotation
        /// </summary>
        /// <returns>Radians</returns>
        public float GetRotation()
        {
            return _rotationInDegrees;
        }


        /// <summary>
        /// Set the rotation in radians
        /// </summary>
        /// <param name="rotation">Rotation in radians</param>
        public void SetRotation(float rotation)
        {
            _rotationInDegrees = rotation;
        }


        /// <summary>
        /// Get the current position
        /// </summary>
        /// <returns>Position as a vector</returns>
        public Vector2 GetPosition()
        {
            return _texturePosition;
        }


        /// <summary>
        /// Change the location
        /// </summary>
        /// <param name="x">X location</param>
        /// <param name="y">Y location</param>
        /// <param name="add">If the position needs to be added or not</param>
        public void SetPosition(float x, float y, bool add)
        {
            if (add)
            {
                _texturePosition.X += x;
                _texturePosition.Y += y;
            }
            else
            {
                _texturePosition.X = x;
                _texturePosition.Y = y;
            }
        }


        /// <summary>
        /// Return the texture size, depending on the chosen texture
        /// </summary>
        /// <param name="textureNumber">The texture number (0 = base texture)</param>
        /// <returns>Vector</returns>
        public Vector2 GetTextureDimensions(int textureNumber)
        {
            return new Vector2(_textures[textureNumber].Width, _textures[textureNumber].Height);
        }


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Draw(GameTime gameTime)
        {
        }
        #endregion
    }
}
